package cn.com.hhrcw.enterprise.job;

import cn.com.hhrcw.entity.EnInfo;
import cn.com.hhrcw.entity.OpDeliveryHistory;
import cn.com.hhrcw.entity.OpJob;
import cn.com.hhrcw.service.IEnInfoService;
import cn.com.hhrcw.service.IOpDeliveryHistoryService;
import cn.com.hhrcw.service.IOpJobService;
import cn.com.hhrcw.utils.RedisLockUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: stdss-boot-parent
 * @description: 统计企业计划招聘人数/待招人数
 * @author: JD
 * @create: 2020-07-28 16:09
 **/
@Component("EnJobStatisticsTask")
@Slf4j
public class EnJobStatisticsTask implements Job {

    private IOpJobService jobService;

    private IEnInfoService enInfoService;

    private IOpDeliveryHistoryService deliveryHistoryService;

    @Autowired
    public void setDeliveryHistoryService(IOpDeliveryHistoryService deliveryHistoryService) {
        this.deliveryHistoryService = deliveryHistoryService;
    }

    @Autowired
    public void setRpProjectService(IOpJobService jobService) {
        this.jobService = jobService;
    }

    @Autowired
    public void setEnInfoService(IEnInfoService enInfoService) {
        this.enInfoService = enInfoService;
    }

    @Override
    public void execute(JobExecutionContext context) {
        String key1 = "JOB_STATISTICS__TASK";
        try {
            if (RedisLockUtils.tryLock(key1, 1, TimeUnit.MINUTES)) {
                LambdaQueryWrapper<EnInfo> enInfoQuery = new LambdaQueryWrapper<>();
                enInfoQuery.eq(EnInfo::getDelFlag, false);
                List<EnInfo> enInfos = enInfoService.list(enInfoQuery);
                if (!CollectionUtils.isEmpty(enInfos)) {
                    for (EnInfo enInfo : enInfos) {
                        LambdaQueryWrapper<OpJob> jobQuery = new LambdaQueryWrapper<>();
                        jobQuery.eq(OpJob::getDelFlag, false).eq(OpJob::getCompanyId, enInfo.getId()).eq(OpJob::getRecruitmentState, 0);
                        List<OpJob> jobs = jobService.list(jobQuery);
                        int dCount = deliveryHistoryService.lambdaQuery().eq(OpDeliveryHistory::getDelFlag, false).eq(OpDeliveryHistory::getCompanyId, enInfo.getId()).count();
                        enInfo.setDeliverNum(dCount);

                        if (!CollectionUtils.isEmpty(jobs)) {
                            enInfo.setPostNum(jobs.size());
                            int planCount = 0;
                            List<String> jobId = new ArrayList<>();
                            for (OpJob job : jobs) {
                                jobId.add(job.getId());
                                planCount = planCount + job.getNeedCount();
                            }
                            enInfo.setPlanCount(planCount);
                            LambdaQueryWrapper<OpDeliveryHistory> historyQuery = new LambdaQueryWrapper<>();
                            historyQuery.eq(OpDeliveryHistory::getDelFlag, false).eq(OpDeliveryHistory::getCompanyId, enInfo.getId())
                                    .eq(OpDeliveryHistory::getState, 6);
                            int alreadyCount = deliveryHistoryService.count(historyQuery);
                            enInfo.setRealCount(alreadyCount);
                        } else {
                            enInfo.setPostNum(0);
                            enInfo.setPlanCount(0);
                            enInfo.setRealCount(0);
                        }
                    }
                    enInfoService.updateBatchById(enInfos);
                }
            }
        }  finally {
            RedisLockUtils.unLock(key1);
        }
    }


}
